import { INTEGER_PG } from "~/db.pg";
import { Sortable } from "~/search.match";
import type { PageDto, PageData, PutRes } from "~/rest";
import { WithPasterList, WithStockTotal } from "~/view.with";

import { Gateway, getGateway } from "@/droplet-bus/core.define";
import { Book } from "@/pond-book/schema.book";

import { StockNode } from "@/river-stock/schema.node";
import { ENUM_TON_NOTCH } from "@/river-stock/core.define";
import { RiverTonGateway } from "@/river-ton/core.define";
import { TonOrder } from "@/river-ton/schema.order";
import { TonSku } from "@/river-ton/schema.sku";

type TonSkuComGetDto = PageDto<TonSku> & { orderId: TonOrder["uuid32"]; decimal: number };
type TonSkuComGetRes = PageData<WithPasterList & TonSku>;

export const TonSkuGateway: Gateway = getGateway(RiverTonGateway, "river_ton_sku");
export type getTonSkuByOrderIdDto = TonSkuComGetDto;
export type getTonSkuByOrderIdRes = TonSkuComGetRes & { justOrder: TonOrder; comboBooks: Book[] };

export type getTonSkuByIdDto = { id: TonSku["id"] };
export type getTonSkuByIdRes = PutRes<TonSku>;

export type searchTonSkuDto = PageDto<TonSku> & { orderGroupId: INTEGER_PG; decimal: number };
export type searchTonSkuRes = TonSkuComGetRes;

export type getTonSkuByStockNodeDto = PageDto<TonSku> & {
  /** 仅查看有剩余库存大于零的商品 */
  isBeyond: boolean;
  decimal: number;
  stockNodeId: StockNode["id"];

  /** 排序 */
  sorter: Sortable;
  notchType: ENUM_TON_NOTCH;
};
// export type getTonSkuByStockNodeRes = PageData<WithPasterList & WithStockTotal & TonSku>;

export interface TonSkuReadInterfaceWithGrant<T> {
  /** 根据订单 - 查询商品 */
  getByOrderId(dto: T & getTonSkuByOrderIdDto): Promise<getTonSkuByOrderIdRes>;

  /** 加工的时候查询单个材料 */
  getById(dto: T & getTonSkuByIdDto): Promise<getTonSkuByIdRes>;

  /** 搜索商品明细 */
  search(dto: T & searchTonSkuDto): Promise<searchTonSkuRes>;
}
